<?php

// +----------------------------------------------------------------------
// | MyCentOS
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.mycentos.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 琥珀 <317559272@qqq.com><http://www.mycentos.com>
// +----------------------------------------------------------------------
// | @date  : 2015-04-09 11:07:40
// +----------------------------------------------------------------------
// | @Last Modified by:  琥珀
// +----------------------------------------------------------------------
// | @Last Modified time: 2015-04-27 15:31:28
// +-----------------------------------------------------------------------


/**
 * 公共函数库
 */

//打印函数
function p($value) {
    echo "<pre>";
    print_r($value);
    echo "</pre>";
}
function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) {

    // 创建Tree
    $tree = array();
    if (is_array($list)) {

        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] = & $list[$key];
        }
        foreach ($list as $key => $data) {

            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] = & $list[$key];
            } else {
                if (isset($refer[$parentId])) {
                    $parent = & $refer[$parentId];
                    $parent[$child][] = & $list[$key];
                }
            }
        }
    }
    return $tree;
}
function getallchildcateids($categoryID) {

    //初始化ID数组
    $array[] = $categoryID;
    do {
        $ids = '';
        $where['pid'] = array('in', $categoryID);
        $cate = M('Category')->where($where)->select();
        foreach ($cate as $k => $v) {
            $array[] = $v['id'];
            $ids.= ',' . $v['id'];
        }
        $ids = substr($ids, 1, strlen($ids));
        $categoryID = $ids;
    } while (!empty($cate));
    $ids = implode(',', $array);

    return $array;
}

//获取分类名称
function get_category_name($category_id = null) {
    if (!$category_id) {
        return;
    }
    $map['id'] = $category_id;
    $category_name = M('Category')->where($map)->getField('name');
    return $category_name;
}
function get_now($id) {
    $cate = M("Category");
    $levels = $cate->field("id,pid,name")->where(array('id'=>$id))->find();
    if ($levels['pid'] != 0) {
       $position.= get_now($levels['pid']);
    }
    $position.= '<li><a href="'.U('Index/index',array('category'=>$levels['id'])).'">' . $levels['name'] . "</a></li>";
    return $position;
}

?>